package leetcode.剑指offer.剑指II;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author :songyaozhang
 * @date :Created 2021/8/11 10:27
 * @description :https://leetcode-cn.com/problems/WhsWhI/
 */
public class 剑指_II_119_最长连续序列 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1,1,2,5,7,9}));
    }

    class Solution {
        public int longestConsecutive(int[] nums) {
            int n = nums.length;
            if (n == 0) return 0;
            Arrays.sort(nums);
            int l = 0, r = l + 1;
            int max = 1;
            int count = 0;
            while (r < n) {
                if (nums[r] - nums[r - 1] == 1) r++;
                else if (nums[r] == nums[r - 1]) {
                    //统计重复元素
                    count++;
                    r++;
                }
                else {
                    max = Math.max(max, r - l - count);
                    count = 0;
                    l = r;
                    r++;
                }
            }
            max = Math.max(max, r - l - count);
            return max;
        }
    }
}
